<?php

/**
 * transacciones actions.
 *
 * @package    dirsac
 * @subpackage transacciones
 * @author     Miguel Flores
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class transaccionesActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->transaccions = Doctrine::getTable('Transaccion')
      ->createQuery('a')
      ->execute();
  }

	public function executeNew(sfWebRequest $request)
	{
		$this->form = new TransaccionForm();
		if ($request->getParameter('compra_id') != 0) {
			$this->compra = Doctrine::getTable($request->getParameter('modulo'))->find($request->getParameter('compra_id'));	
		}
		else {
			$this->compra = 0;	
		}
		
		$this->modulo = $request->getParameter('modulo');
		$this->tipo = $request->getParameter('tipo');
		$this->cuenta = Doctrine::getTable('Cuenta')->find($request->getParameter('id'));
	}

	public function executeCreate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST));

		$this->form = new TransaccionForm();

		$this->processForm($request, $this->form);

		$this->setTemplate('new');
	}

	public function executeEdit(sfWebRequest $request)
	{
		$this->forward404Unless($transaccion = Doctrine::getTable('Transaccion')->find(array($request->getParameter('id'))), sprintf('Object transaccion does not exist (%s).', $request->getParameter('id')));
		$this->form = new TransaccionForm($transaccion);
	}

	public function executeUpdate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
		$this->forward404Unless($transaccion = Doctrine::getTable('Transaccion')->find(array($request->getParameter('id'))), sprintf('Object transaccion does not exist (%s).', $request->getParameter('id')));
		$this->form = new TransaccionForm($transaccion);

		$this->processForm($request, $this->form);

		$this->setTemplate('edit');
	}

	public function executeDelete(sfWebRequest $request)
	{
		$request->checkCSRFProtection();

		$this->forward404Unless($transaccion = Doctrine::getTable('Transaccion')->find(array($request->getParameter('id'))), sprintf('Object transaccion does not exist (%s).', $request->getParameter('id')));
		$transaccion->delete();

		$this->redirect('transacciones/index');
	}

	protected function processForm(sfWebRequest $request, sfForm $form)
	{
		$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
		if ($form->isValid())
		{
			$transaccion = $form->save();
			//$this->redirect('transacciones/edit?id='.$transaccion->getId());
			
			$modulo = Dirsac::getModulo($transaccion->getModulo());
			
			if ($modulo == '/admin/cuentas') {
				$this->redirect($modulo.'/transacciones?id='.$transaccion->getCuentaId());	
			}
			else {
				$this->redirect($modulo.'/transacciones?id='.$transaccion->getCompraId());
			}
		}
	}
}
